package com.iris.live.services.services.impl;

import com.google.common.base.Strings;
import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.common.WeekOfMonthUtils;
import com.iris.live.services.data.models.*;
import com.iris.live.services.data.repositories.*;
import com.iris.live.services.models.PageData;
import com.iris.live.services.models.ResultData;
import com.iris.live.services.services.CrossDepartmentTacticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lusong on 2016/11/25.
 */
@Service

public class CrossDepartmentTacticsServiceImpl implements CrossDepartmentTacticsService {

    private static Logger logger = LoggerFactory.getLogger(CrossDepartmentTacticsServiceImpl.class);

    @Autowired
    private 线索统计Repository repository线索统计;

    @Autowired
    private 月度设定目标Repository 月度设定目标Repository;

    @Autowired
    private 跨部门方案Repository repository跨部门方案;

    @Autowired
    private CrossDepartmentTacticsRepository crossDepartmentTacticsRepository;

    @Autowired
    private 每月各责任部门目标Repository repository每月各责任部门目标;

    @Autowired
    private 每月销售顾问目标Repository repository每月销售顾问目标;

    @Autowired
    private 每周各责任部门目标Repository repository每周各责任部门目标;

    @Autowired
    private 每周销售顾问目标Repository repository每周销售顾问目标;

    @Autowired
    private ApiForLiveRepository apiForLiveRepository;

    @Autowired
    private 每周设定目标Repository repository每周设定目标;

    @Override
    public ResultData weekActualValue(String permit, RealInfo realInfo) {
        ResultData resultData = new ResultData();

        String year = realInfo.year;
        String month = realInfo.month;
        String carCode = realInfo.carCode;
        String beginDay = realInfo.beginDay;
        String endDay = realInfo.endDay;
        Integer day = Integer.parseInt(realInfo.days);//本周天数

        if (realInfo.carCode != null && realInfo.carCode.equals("总计")) {
            carCode = "";
        }
        String groupType = realInfo.groupType;

        try {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();

            calendar.setTime(sdf2.parse(beginDay));
            Integer days = calendar.getActualMaximum(Calendar.DATE);//本月天数
            Map map = new HashMap();
            String ym = beginDay.substring(0, 6);

            // 依据年月获取日期范围
            String month_Begin_Date = getMonthDateRange(year,month,0);
            String month_End_Date = getMonthDateRange(year,month,1);

            List<Object> resultSet = crossDepartmentTacticsRepository.findResultAboutPermitAndYMAndCarCodeWeek(permit, ym, carCode, "活跃,休眠", "0", day, days);
            Map mObj;
            String channel = null;
            List activeClues = new ArrayList<>(), sleepingClues = new ArrayList<>();
            for(Object rs : resultSet){
                mObj = (Map) rs;
                if(mObj.get("渠道") == null || mObj.get("渠道").toString().equals("")) continue;

                channel = mObj.get("渠道").toString() + "线索数";
                mObj.put(channel, mObj.get("有效线索数"));
                if(mObj.get("渠道").toString().equals("活跃")){
                    activeClues.add(mObj);
                }else if(mObj.get("渠道").toString().equals("休眠")){
                    sleepingClues.add(mObj);
                }
                mObj.remove("有效线索数");
                mObj.remove("渠道");
            }
            map.put("活跃线索数", activeClues);
            map.put("休眠线索数", sleepingClues);

            /*map.put("活跃线索数", crossDepartmentTacticsRepository.findResultAboutPermitAndYMAndCarCodeWeek(permit, ym, carCode, "活跃", "0", day, days));
            map.put("休眠线索数", crossDepartmentTacticsRepository.findResultAboutPermitAndYMAndCarCodeWeek(permit, ym, carCode, "休眠", "0", day, days));*/

             /* 获取有效线索数 推荐 在购 呼入 网络 主动集客*/
            resultSet = crossDepartmentTacticsRepository.getWeekClueNum(permit, month_Begin_Date, beginDay, endDay, groupType, day, days);
            List recommends = new ArrayList<>(), repurchases = new ArrayList<>(), nets = new ArrayList<>(), incalls = new ArrayList<>(), gathers = new ArrayList<>();
            for(Object rs : resultSet){
                mObj = (Map) rs;
                if(mObj.get("渠道") == null || mObj.get("渠道").toString().equals("")) continue;

                channel = mObj.get("渠道").toString() + "线索数";
                mObj.put(channel, mObj.get("有效线索数"));
                if(mObj.get("渠道").toString().equals("推荐")){
                    recommends.add(mObj);
                }else if(mObj.get("渠道").toString().equals("再购")){
                    repurchases.add(mObj);
                }else if(mObj.get("渠道").toString().equals("网络")){
                    nets.add(mObj);
                }else if(mObj.get("渠道").toString().equals("主动集客")){
                    gathers.add(mObj);
                }else if(mObj.get("渠道").toString().equals("呼入")){
                    incalls.add(mObj);
                }
                mObj.remove("有效线索数");
                mObj.remove("渠道");
            }
            map.put("推荐线索数", recommends);
            map.put("再购线索数", repurchases);
            map.put("网络线索数", nets);
            map.put("主动集客线索数", gathers);
            map.put("呼入线索数", incalls);

            /*散客客流数、散客留档数及进店数*/
            resultSet = crossDepartmentTacticsRepository.getToShopClueNum(permit, beginDay, endDay, groupType);
            map = getActiveClueNum(map, resultSet);

            String startDate = DateFormatUtils.getFormatDateToString(DateFormatUtils.getFormatDate(beginDay, DateFormatUtils.TIME_FORMAT_STR_YYYYMMDD) ,DateFormatUtils.TIME_FORMAT_STR_YYYY_MM_DD);
            String endDate = DateFormatUtils.getFormatDateToString(DateFormatUtils.getFormatDate(endDay, DateFormatUtils.TIME_FORMAT_STR_YYYYMMDD) ,DateFormatUtils.TIME_FORMAT_STR_YYYY_MM_DD);
            resultSet = crossDepartmentTacticsRepository.getOtherClueNum(permit, startDate, endDate, groupType);
            map = getOtherNum(map, resultSet);


            List list = new ArrayList();
            list.add(map);
            PageData pageData = new PageData(list, 0);
            resultData.setResult("true");
            resultData.setPageData(pageData);
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("程序异常");
            logger.error("查询跨部门周实际数据异常", e);
        }
        return resultData;
    }


    @Override
    public ResultData lastMonthActualValue(String permit, RealInfo realInfo) {
        ResultData resultData = new ResultData();

        String year = realInfo.year;
        String month = realInfo.month;
        String groupType = realInfo.groupType;

        try {
            Map map = new HashMap();
            String ym = null;
            if (month != null && month.length() == 1) {
                ym = year + "0" + month;
            } else {
                ym = year + month;
            }

            // 依据年月获取日期范围
            String month_Begin_Date = getMonthDateRange(year,month,0);
            String month_End_Date = getMonthDateRange(year,month,1);

            List<Object> resultSet = null;
            Map mObj;
            String channel = null;
            List activeClues = new ArrayList<>(), sleepingClues = new ArrayList<>(), recommends = new ArrayList<>(),
                    repurchases = new ArrayList<>(), nets = new ArrayList<>(), incalls = new ArrayList<>(), gathers = new ArrayList<>();

            resultSet = crossDepartmentTacticsRepository.findResultAboutPermitAndYMAndCarCode(permit, ym, null, "活跃,休眠", groupType);
            for(Object rs : resultSet){
                mObj = (Map) rs;
                if(mObj.get("渠道") == null || mObj.get("渠道").toString().equals("")) continue;

                channel = mObj.get("渠道").toString() + "线索数";
                mObj.put(channel, mObj.get("有效线索数"));
                if(mObj.get("渠道").toString().equals("活跃")){
                    activeClues.add(mObj);
                }else if(mObj.get("渠道").toString().equals("休眠")){
                    sleepingClues.add(mObj);
                }
                mObj.remove("有效线索数");
                mObj.remove("渠道");
            }
            map.put("活跃线索数", activeClues);
            map.put("休眠线索数", sleepingClues);

            resultSet = crossDepartmentTacticsRepository.getClueNumOfMonth(permit, month_Begin_Date, groupType);
            for(Object rs : resultSet){
                mObj = (Map) rs;
                Map clueMap = new HashMap();
                if(mObj.get("渠道") == null || mObj.get("渠道").toString().equals("")) continue;

                channel = mObj.get("渠道").toString() + "线索数";
                clueMap.put(channel, mObj.get("有效线索数"));
                if(mObj.get("渠道").toString().equals("推荐")){
                    recommends.add(clueMap);
                }else if(mObj.get("渠道").toString().equals("再购")){
                    repurchases.add(clueMap);
                }else if(mObj.get("渠道").toString().equals("网络")){
                    nets.add(clueMap);
                }else if(mObj.get("渠道").toString().equals("主动集客")){
                    gathers.add(clueMap);
                }else if(mObj.get("渠道").toString().equals("呼入")){
                    incalls.add(clueMap);
                }
            }
            map.put("推荐线索数", recommends);
            map.put("再购线索数", repurchases);
            map.put("网络线索数", nets);
            map.put("主动集客线索数", gathers);
            map.put("呼入线索数", incalls);

            /*散客客流数、散客留档数及进店数*/
            resultSet = crossDepartmentTacticsRepository.getToShopClueNum(permit, month_Begin_Date, month_End_Date, groupType);
            map = getActiveClueNum(map, resultSet);

            String startDate = DateFormatUtils.getFormatDateToString(DateFormatUtils.getFormatDate(month_Begin_Date, DateFormatUtils.TIME_FORMAT_STR_YYYYMMDD) ,DateFormatUtils.TIME_FORMAT_STR_YYYY_MM_DD);
            String endDate = DateFormatUtils.getFormatDateToString(DateFormatUtils.getFormatDate(month_End_Date, DateFormatUtils.TIME_FORMAT_STR_YYYYMMDD) ,DateFormatUtils.TIME_FORMAT_STR_YYYY_MM_DD);
            resultSet = crossDepartmentTacticsRepository.getOtherClueNum(permit, startDate, endDate, groupType);
            map = getOtherNum(map, resultSet);

            List list = new ArrayList();
            list.add(map);
            PageData pageData = new PageData(list, 0);
            resultData.setResult("true");
            resultData.setPageData(pageData);
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("程序异常");
            logger.error("查询跨部门上月实际数据异常", e);
        }
        return resultData;
    }

    @Override
    public ResultData getSalesGroup(String permit) {
        ResultData resultData = new ResultData();
        try {
            List list = crossDepartmentTacticsRepository.getSalesGroup(permit);
            resultData.setPageData(new PageData(list, 0));
            resultData.setResult("true");
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("程序异常");
            logger.error("查询销售顾问分组异常", e);
        }
        return resultData;
    }

    @Override
    public ResultData getAimValue(String year, String month, String permit) {
        ResultData resultData = new ResultData();
        try {
            List list = crossDepartmentTacticsRepository.getAimValue(year, month, permit);
            resultData.setPageData(new PageData(list, 0));
            resultData.setResult("true");
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("程序异常");
            logger.error("查询月度设定目标异常", e);
        }
        return resultData;
    }

    @Override
    @Transactional
    public boolean saveAimValue(月度设定目标Model 月度设定目标Model) {
        boolean flag = false;
        try {
            this.月度设定目标Repository.save(月度设定目标Model);
            flag = true;
        } catch (Exception e) {
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
            logger.error("保存月度设定目标异常", e);
        }

        return flag;
    }

    @Override
    @Transactional
    public ResultData findRelevantDepartment(String permit, String yearMonth, String carCode) {
        ResultData resultData = new ResultData();
        try {
            List<跨部门方案Model> list = repository跨部门方案.findRelevantDepartment(permit, yearMonth, carCode);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, list != null ? list.size() : 0));
        } catch (Exception e) {
            resultData.setResult("false");
            logger.error("查询结果责任部门异常", e);
        }
        return resultData;
    }

    @Override
    @Transactional
    public ResultData findActionPlan(String permit, String yearMonth, String carCode) {
        ResultData resultData = new ResultData();
        try {
            List<跨部门方案Model> list = repository跨部门方案.findActionPlan(permit, yearMonth, carCode);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, list != null ? list.size() : 0));
        } catch (Exception e) {
            resultData.setResult("false");
            logger.error("查询行动提升计划异常", e);
        }
        return resultData;
    }

    @Override
    @Transactional
    public ResultData saveActionPlan(String permit, List<GetActionPlan> list) {
        ResultData resultData = new ResultData();
        try {
            if (list != null && list.size() > 0) {
                List<跨部门方案Model> listModel = repository跨部门方案.findActionPlan(permit, list.get(0).yearMonth, list.get(0).carCode);
                repository跨部门方案.deleteInBatch(listModel);
                for (GetActionPlan getActionPlan : list) {
                    跨部门方案Model model = new 跨部门方案Model();
                    model.set许可(permit);
                    model.set年月(getActionPlan.yearMonth);
                    model.set车型(getActionPlan.carCode);
                    model.set行动提升方案(getActionPlan.行动提升方案);
                    model.set启用时间(getActionPlan.启用时间);
                    model.set结束时间(getActionPlan.结束时间);
                    model.set负责人(getActionPlan.负责人);
                    model.set第一周落实结果(getActionPlan.第一周落实结果);
                    model.set第二周落实结果(getActionPlan.第二周落实结果);
                    model.set第三周落实结果(getActionPlan.第三周落实结果);
                    model.set第四周落实结果(getActionPlan.第四周落实结果);
                    model.set第五周落实结果(getActionPlan.第五周落实结果);
                    model.set更新时间(new Timestamp(new Date().getTime()));
                    repository跨部门方案.save(model);
                }
            }
            resultData.setResult("true");
        } catch (Exception e) {
            resultData.setResult("false");
            logger.error("保存行动提升计划异常", e);
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        }
        return resultData;
    }


    @Override
    @Transactional
    public ResultData saveRelevantDepartment(String permit, GetRelevantDepartment getRelevantDepartment) {
        ResultData resultData = new ResultData();
        try {
            List<跨部门方案Model> list = repository跨部门方案.findRelevantDepartment(permit, getRelevantDepartment.yearMonth, getRelevantDepartment.carCode);
            repository跨部门方案.deleteInBatch(list);

            //展厅转化交车数
            跨部门方案Model m1 = new 跨部门方案Model();
            m1.set许可(permit);
            m1.set年月(getRelevantDepartment.yearMonth);
            m1.set车型(getRelevantDepartment.carCode);
            m1.set结果责任部门(getRelevantDepartment.show);
            m1.set项目("展厅转化交车数");
            m1.set更新时间(new Timestamp(new Date().getTime()));
            repository跨部门方案.save(m1);
            //展厅转化订单数
            跨部门方案Model m2 = new 跨部门方案Model();
            m2.set许可(permit);
            m2.set年月(getRelevantDepartment.yearMonth);
            m2.set车型(getRelevantDepartment.carCode);
            m2.set项目("展厅转化订单数");
            m2.set结果责任部门(getRelevantDepartment.show);
            m2.set更新时间(new Timestamp(new Date().getTime()));
            repository跨部门方案.save(m2);
            //展厅转化报价数
            跨部门方案Model m3 = new 跨部门方案Model();
            m3.set许可(permit);
            m3.set年月(getRelevantDepartment.yearMonth);
            m3.set车型(getRelevantDepartment.carCode);
            m3.set项目("展厅转化报价数");
            m3.set结果责任部门(getRelevantDepartment.show);
            repository跨部门方案.save(m3);
            //展厅转化进店线索数
            跨部门方案Model m4 = new 跨部门方案Model();
            m4.set许可(permit);
            m4.set年月(getRelevantDepartment.yearMonth);
            m4.set车型(getRelevantDepartment.carCode);
            m4.set项目("展厅转化进店线索数");
            m4.set结果责任部门(null);
            repository跨部门方案.save(m4);
            //展厅转化线索数
            跨部门方案Model m5 = new 跨部门方案Model();
            m5.set许可(permit);
            m5.set年月(getRelevantDepartment.yearMonth);
            m5.set车型(getRelevantDepartment.carCode);
            m5.set项目("展厅转化线索数");
            m5.set结果责任部门(null);
            repository跨部门方案.save(m5);
            //活跃进店线索数
            跨部门方案Model m6 = new 跨部门方案Model();
            m6.set许可(permit);
            m6.set年月(getRelevantDepartment.yearMonth);
            m6.set车型(getRelevantDepartment.carCode);
            m6.set项目("活跃进店线索数");
            m6.set结果责任部门(getRelevantDepartment.activeToShop);
            repository跨部门方案.save(m6);
            //活跃线索数
            跨部门方案Model m7 = new 跨部门方案Model();
            m7.set许可(permit);
            m7.set年月(getRelevantDepartment.yearMonth);
            m7.set车型(getRelevantDepartment.carCode);
            m7.set项目("活跃线索数");
            m7.set结果责任部门(getRelevantDepartment.active);
            repository跨部门方案.save(m7);
            //休眠进店线索数
            跨部门方案Model m8 = new 跨部门方案Model();
            m8.set许可(permit);
            m8.set年月(getRelevantDepartment.yearMonth);
            m8.set车型(getRelevantDepartment.carCode);
            m8.set项目("休眠进店线索数");
            m8.set结果责任部门(getRelevantDepartment.sleepToShop);
            repository跨部门方案.save(m8);
            //休眠线索数
            跨部门方案Model m9 = new 跨部门方案Model();
            m9.set许可(permit);
            m9.set年月(getRelevantDepartment.yearMonth);
            m9.set车型(getRelevantDepartment.carCode);
            m9.set项目("休眠线索数");
            m9.set结果责任部门(getRelevantDepartment.sleep);
            repository跨部门方案.save(m9);
            //推荐进店线索数
            跨部门方案Model m10 = new 跨部门方案Model();
            m10.set许可(permit);
            m10.set年月(getRelevantDepartment.yearMonth);
            m10.set车型(getRelevantDepartment.carCode);
            m10.set项目("推荐进店线索数");
            m10.set结果责任部门(getRelevantDepartment.groomToShop);
            repository跨部门方案.save(m10);
            //推荐线索数
            跨部门方案Model m11 = new 跨部门方案Model();
            m11.set许可(permit);
            m11.set年月(getRelevantDepartment.yearMonth);
            m11.set车型(getRelevantDepartment.carCode);
            m11.set项目("推荐线索数");
            m11.set结果责任部门(getRelevantDepartment.groom);
            repository跨部门方案.save(m11);
            //再购进店线索数
            跨部门方案Model m12 = new 跨部门方案Model();
            m12.set许可(permit);
            m12.set年月(getRelevantDepartment.yearMonth);
            m12.set车型(getRelevantDepartment.carCode);
            m12.set项目("再购进店线索数");
            m12.set结果责任部门(getRelevantDepartment.againToShopBuy);
            repository跨部门方案.save(m12);
            //再购线索数
            跨部门方案Model m13 = new 跨部门方案Model();
            m13.set许可(permit);
            m13.set年月(getRelevantDepartment.yearMonth);
            m13.set车型(getRelevantDepartment.carCode);
            m13.set项目("再购线索数");
            m13.set结果责任部门(getRelevantDepartment.againBuy);
            repository跨部门方案.save(m13);
            //散客留档数
            跨部门方案Model m14 = new 跨部门方案Model();
            m14.set许可(permit);
            m14.set年月(getRelevantDepartment.yearMonth);
            m14.set车型(getRelevantDepartment.carCode);
            m14.set项目("散客留档数");
            m14.set结果责任部门(getRelevantDepartment.individua);
            repository跨部门方案.save(m14);
            //散客客流
            跨部门方案Model m15 = new 跨部门方案Model();
            m15.set许可(permit);
            m15.set年月(getRelevantDepartment.yearMonth);
            m15.set车型(getRelevantDepartment.carCode);
            m15.set项目("散客客流");
            m15.set结果责任部门(getRelevantDepartment.individuaIo);
            repository跨部门方案.save(m15);
            //呼入进店线索数
            跨部门方案Model m16 = new 跨部门方案Model();
            m16.set许可(permit);
            m16.set年月(getRelevantDepartment.yearMonth);
            m16.set车型(getRelevantDepartment.carCode);
            m16.set项目("呼入进店线索数");
            m16.set结果责任部门(getRelevantDepartment.incomingToShop);
            repository跨部门方案.save(m16);
            //呼入线索数
            跨部门方案Model m17 = new 跨部门方案Model();
            m17.set许可(permit);
            m17.set年月(getRelevantDepartment.yearMonth);
            m17.set车型(getRelevantDepartment.carCode);
            m17.set项目("呼入线索数");
            m17.set结果责任部门(getRelevantDepartment.incoming);
            repository跨部门方案.save(m17);
            //网络进店线索数
            跨部门方案Model m18 = new 跨部门方案Model();
            m18.set许可(permit);
            m18.set年月(getRelevantDepartment.yearMonth);
            m18.set车型(getRelevantDepartment.carCode);
            m18.set项目("网络进店线索数");
            m18.set结果责任部门(getRelevantDepartment.networkToShop);
            repository跨部门方案.save(m18);
            //网络线索数
            跨部门方案Model m19 = new 跨部门方案Model();
            m19.set许可(permit);
            m19.set年月(getRelevantDepartment.yearMonth);
            m19.set车型(getRelevantDepartment.carCode);
            m19.set项目("网络线索数");
            m19.set结果责任部门(getRelevantDepartment.newWork);
            repository跨部门方案.save(m19);
            //主动集客进店线索数
            跨部门方案Model m20 = new 跨部门方案Model();
            m20.set许可(permit);
            m20.set年月(getRelevantDepartment.yearMonth);
            m20.set车型(getRelevantDepartment.carCode);
            m20.set项目("主动集客进店线索数");
            m20.set结果责任部门(getRelevantDepartment.drivingToShop);
            repository跨部门方案.save(m20);
            //主动集客线索数
            跨部门方案Model m21 = new 跨部门方案Model();
            m21.set许可(permit);
            m21.set年月(getRelevantDepartment.yearMonth);
            m21.set车型(getRelevantDepartment.carCode);
            m21.set项目("主动集客线索数");
            m21.set结果责任部门(getRelevantDepartment.driving);
            repository跨部门方案.save(m21);
            resultData.setResult("true");
        } catch (Exception e) {
            resultData.setResult("false");
            logger.error("保存结果责任部门异常", e);
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        }
        return resultData;
    }

    public ResultData findCountAboutWingAndCusAndShow(String permit, String yearMonth, String carCode) {
        ResultData resultData = new ResultData();
        try {
            Map map = new HashMap();
            map.put("展厅交车数", crossDepartmentTacticsRepository.findTeslaCountAboutWingAndCusAndShow(permit, yearMonth, "展厅"));
            map.put("大客户交车数", crossDepartmentTacticsRepository.findTeslaCountAboutWingAndCusAndShow(permit, yearMonth, "大客户"));
            map.put("车展交车数", 0);

            map.put("展厅订单数", crossDepartmentTacticsRepository.findOrderCountAboutWingAndCusAndShow(permit, yearMonth, "展厅"));
            map.put("大客户订单数", crossDepartmentTacticsRepository.findOrderCountAboutWingAndCusAndShow(permit, yearMonth, "大客户"));
            map.put("车展订单数", 0);
            List list = new ArrayList();
            list.add(map);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 0));
        } catch (Exception e) {
            resultData.setResult("false");
            logger.error("查询展厅、大客户、车展的订单数和交车数", e);
        }
        return resultData;
    }

    /**
     * 查询 经销商下所有用户
     *
     * @param permit
     * @return
     */
    public ResultData findUserInfoByPermit(String permit) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            resultData.setResult("true");
            int num = crossDepartmentTacticsRepository.findUserInfoByPermit(permit) != null ? crossDepartmentTacticsRepository.findUserInfoByPermit(permit).size() : 0;
            resultData.setPageData(new PageData(crossDepartmentTacticsRepository.findUserInfoByPermit(permit), num));
        } catch (Exception e) {
            logger.error("查询经销商下所有用户异常", e);
        }
        return resultData;
    }

    public ResultData findMonthDepartmentTarget(String permit, String yearMonth, String carCode) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            List<每月各责任部门目标Model> list = repository每月各责任部门目标.findModelByPermitAndYMAndCarCode(permit, yearMonth, carCode);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 1));
        } catch (Exception e) {
            logger.error("查询每月各责任部门目标异常", e);
        }
        return resultData;
    }

    public ResultData findMonthSalesTarget(String permit, String yearMonth, String carCode) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            List<每月销售顾问目标Model> list = repository每月销售顾问目标.findModelByPermitAndYMAndCarCode(permit, yearMonth, carCode);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 1));
        } catch (Exception e) {
            logger.error("查询每月销售顾问目标异常", e);
        }
        return resultData;
    }

    public ResultData saveMonthDepartmentTarget(String permit, String username, List<GetMonthDepartmentTarget> list) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            if (list != null & list.size() > 0) {
                //若 第N周 为空或者null，删除相关月报信息，否则删除相关周报信息
                if (list.get(0).第N周 != null && !"".equals(list.get(0).第N周)) {
                    List<每周各责任部门目标Model> listModel = repository每周各责任部门目标.findCountByPermitAndYMAndCarCodeAndWeek(permit, list.get(0).年月, list.get(0).车型, list.get(0).第N周);
                    repository每周各责任部门目标.deleteInBatch(listModel);
                } else {
                    List<每月各责任部门目标Model> listModel = repository每月各责任部门目标.findModelByPermitAndYMAndCarCode(permit, list.get(0).年月, list.get(0).车型);
                    repository每月各责任部门目标.deleteInBatch(listModel);
                }
                for (GetMonthDepartmentTarget getMonthDepartmentTarget : list) {
                    //若 第N周 为空或者null，保存月报信息，否则保存周报信息
                    if (getMonthDepartmentTarget.第N周 != null && !"".equals(getMonthDepartmentTarget.第N周)) {
                        每周各责任部门目标Model model = new 每周各责任部门目标Model();
                        model.set许可(permit);
                        model.set年月(getMonthDepartmentTarget.年月);
                        model.set车型(getMonthDepartmentTarget.车型);
                        model.set部门(getMonthDepartmentTarget.部门);
                        model.set第N周(getMonthDepartmentTarget.第N周);
                        model.set散客线索数(getMonthDepartmentTarget.散客线索数);
                        model.set散客进店线索数(getMonthDepartmentTarget.散客进店线索数);
                        model.set再购线索数(getMonthDepartmentTarget.再购线索数);
                        model.set再购进店线索数(getMonthDepartmentTarget.再购进店线索数);
                        model.set呼入线索数(getMonthDepartmentTarget.呼入线索数);
                        model.set呼入进店线索数(getMonthDepartmentTarget.呼入进店线索数);
                        model.set推荐线索数(getMonthDepartmentTarget.推荐线索数);
                        model.set推荐进店线索数(getMonthDepartmentTarget.推荐进店线索数);
                        model.set网络线索数(getMonthDepartmentTarget.网络线索数);
                        model.set网络进店线索数(getMonthDepartmentTarget.网络进店线索数);
                        model.set主动集客线索数(getMonthDepartmentTarget.主动集客线索数);
                        model.set主动集客进店线索数(getMonthDepartmentTarget.主动集客进店线索数);
                        model.set活跃线索数(getMonthDepartmentTarget.活跃线索数);
                        model.set活跃进店线索数(getMonthDepartmentTarget.活跃进店线索数);
                        model.set休眠线索数(getMonthDepartmentTarget.休眠线索数);
                        model.set休眠进店线索数(getMonthDepartmentTarget.休眠进店线索数);
                        model.set更新时间(new Timestamp(new Date().getTime()));
                        model.set更新账号(username);
                        repository每周各责任部门目标.save(model);
                    } else {
                        每月各责任部门目标Model model = new 每月各责任部门目标Model();
                        model.set许可(permit);
                        model.set年月(getMonthDepartmentTarget.年月);
                        model.set车型(getMonthDepartmentTarget.车型);
                        model.set部门(getMonthDepartmentTarget.部门);
                        model.set散客线索数(getMonthDepartmentTarget.散客线索数);
                        model.set散客进店线索数(getMonthDepartmentTarget.散客进店线索数);
                        model.set再购线索数(getMonthDepartmentTarget.再购线索数);
                        model.set再购进店线索数(getMonthDepartmentTarget.再购进店线索数);
                        model.set呼入线索数(getMonthDepartmentTarget.呼入线索数);
                        model.set呼入进店线索数(getMonthDepartmentTarget.呼入进店线索数);
                        model.set推荐线索数(getMonthDepartmentTarget.推荐线索数);
                        model.set推荐进店线索数(getMonthDepartmentTarget.推荐进店线索数);
                        model.set网络线索数(getMonthDepartmentTarget.网络线索数);
                        model.set网络进店线索数(getMonthDepartmentTarget.网络进店线索数);
                        model.set主动集客线索数(getMonthDepartmentTarget.主动集客线索数);
                        model.set主动集客进店线索数(getMonthDepartmentTarget.主动集客进店线索数);
                        model.set活跃线索数(getMonthDepartmentTarget.活跃线索数);
                        model.set活跃进店线索数(getMonthDepartmentTarget.活跃进店线索数);
                        model.set休眠线索数(getMonthDepartmentTarget.休眠线索数);
                        model.set休眠进店线索数(getMonthDepartmentTarget.休眠进店线索数);
                        model.set更新时间(new Timestamp(new Date().getTime()));
                        model.set更新账号(username);
                        repository每月各责任部门目标.save(model);
                    }
                }
            }
            resultData.setResult("true");
        } catch (Exception e) {
            logger.error("保存本月各责任部门目标异常", e);
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        }
        return resultData;
    }

    public ResultData saveMonthSalesTarget(String permit, String username, List<GetMonthSalesTarget> list) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            if (list != null && list.size() > 0) {
                for (GetMonthSalesTarget getMonthSalesTarget : list) {
                    //若 第N周 为空或者null，保存月报信息，否则保存周报信息
                    if (list.get(0).第N周 != null && !"".equals(list.get(0))) {
                        每周销售顾问目标Model model = repository每周销售顾问目标.findCountByPermitAndYMAndCarCodeAndWeekAndScCode(permit, getMonthSalesTarget.年月, getMonthSalesTarget.车型, getMonthSalesTarget.第N周, getMonthSalesTarget.销售顾问);
                        if (model == null) {
                            model = new 每周销售顾问目标Model();
                        }
                        model.set许可(permit);
                        model.set年月(getMonthSalesTarget.年月);
                        model.set车型(getMonthSalesTarget.车型);
                        model.set销售顾问(getMonthSalesTarget.销售顾问);
                        model.set第N周(getMonthSalesTarget.第N周);
                        model.set总线索数(getMonthSalesTarget.总线索数);
                        model.set交车数(getMonthSalesTarget.交车数);
                        model.set订单数(getMonthSalesTarget.订单数);
                        model.set报价数(getMonthSalesTarget.报价数);
                        model.set进店线索数(getMonthSalesTarget.进店线索数);
                        model.set总线索数(getMonthSalesTarget.总线索数);
                        model.set活跃进店线索数(getMonthSalesTarget.活跃进店线索数);
                        model.set休眠进店线索数(getMonthSalesTarget.休眠进店线索数);
                        model.set推荐进店线索数(getMonthSalesTarget.推荐进店线索数);
                        model.set再购进店线索数(getMonthSalesTarget.再购进店线索数);
                        model.set散客进店线索数(getMonthSalesTarget.散客进店线索数);
                        model.set呼入进店线索数(getMonthSalesTarget.呼入进店线索数);
                        model.set网络进店线索数(getMonthSalesTarget.网络进店线索数);
                        model.set主动集客进店线索数(getMonthSalesTarget.主动集客进店线索数);
                        model.set活跃线索数(getMonthSalesTarget.活跃线索数);
                        model.set休眠线索数(getMonthSalesTarget.休眠线索数);
                        model.set推荐线索数(getMonthSalesTarget.推荐线索数);
                        model.set再购线索数(getMonthSalesTarget.再购线索数);
                        model.set散客线索数(getMonthSalesTarget.散客线索数);
                        model.set呼入线索数(getMonthSalesTarget.呼入线索数);
                        model.set网络线索数(getMonthSalesTarget.网络线索数);
                        model.set主动集客线索数(getMonthSalesTarget.主动集客线索数);
                        model.set试驾数(getMonthSalesTarget.试驾数);
                        model.set更新账号(username);
                        model.set更新时间(new Timestamp(new Date().getTime()));
                        repository每周销售顾问目标.save(model);
                    } else {
                        每月销售顾问目标Model model = repository每月销售顾问目标.findModelByPermitAndYMAndCarCodeAndScCode(permit, getMonthSalesTarget.年月, getMonthSalesTarget.车型, getMonthSalesTarget.销售顾问);
                        if (model == null) {
                            model = new 每月销售顾问目标Model();
                        }
                        model.set许可(permit);
                        model.set年月(getMonthSalesTarget.年月);
                        model.set车型(getMonthSalesTarget.车型);
                        model.set销售顾问(getMonthSalesTarget.销售顾问);
                        model.set总线索数(getMonthSalesTarget.总线索数);
                        model.set交车数(getMonthSalesTarget.交车数);
                        model.set订单数(getMonthSalesTarget.订单数);
                        model.set报价数(getMonthSalesTarget.报价数);
                        model.set进店线索数(getMonthSalesTarget.进店线索数);
                        model.set总线索数(getMonthSalesTarget.总线索数);
                        model.set活跃进店线索数(getMonthSalesTarget.活跃进店线索数);
                        model.set休眠进店线索数(getMonthSalesTarget.休眠进店线索数);
                        model.set推荐进店线索数(getMonthSalesTarget.推荐进店线索数);
                        model.set再购进店线索数(getMonthSalesTarget.再购进店线索数);
                        model.set散客进店线索数(getMonthSalesTarget.散客进店线索数);
                        model.set呼入进店线索数(getMonthSalesTarget.呼入进店线索数);
                        model.set网络进店线索数(getMonthSalesTarget.网络进店线索数);
                        model.set主动集客进店线索数(getMonthSalesTarget.主动集客进店线索数);
                        model.set活跃线索数(getMonthSalesTarget.活跃线索数);
                        model.set休眠线索数(getMonthSalesTarget.休眠线索数);
                        model.set推荐线索数(getMonthSalesTarget.推荐线索数);
                        model.set再购线索数(getMonthSalesTarget.再购线索数);
                        model.set散客线索数(getMonthSalesTarget.散客线索数);
                        model.set呼入线索数(getMonthSalesTarget.呼入线索数);
                        model.set网络线索数(getMonthSalesTarget.网络线索数);
                        model.set主动集客线索数(getMonthSalesTarget.主动集客线索数);
                        model.set试驾数(getMonthSalesTarget.试驾数);
                        model.set更新账号(username);
                        model.set更新时间(new Timestamp(new Date().getTime()));
                        repository每月销售顾问目标.save(model);
                    }
                }
            }
            resultData.setResult("true");
        } catch (Exception e) {
            logger.error("保存本月销售顾问目标异常", e);
            TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        }
        return resultData;
    }

    public ResultData getAvailableUser(String permit) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            List<?> list = apiForLiveRepository.getAvailableUser(permit);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 0));
        } catch (Exception e) {
            logger.error("查询可接到用户异常", e);
        }
        return resultData;
    }

    public ResultData getAlreadyReceptionUser(String permit, String yearMonth) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            List<?> list = crossDepartmentTacticsRepository.getAlreadyReceptionUser(permit, yearMonth);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 0));
        } catch (Exception e) {
            logger.error("查询可接到用户异常", e);
        }
        return resultData;
    }

    public ResultData findWeekDepartmentTarget(String permit, String yearMonth, String carCode, String week) {
        ResultData resultData = new ResultData("false", null, null);
        List<?> list = null;
        if (yearMonth == null || "".equals(yearMonth)) {
            logger.error("yearMonth为空或null");
        }
        if (carCode == null || "".equals(carCode)) {
            logger.error("carCode为空或null");
        }
        if (week == null || "".equals(week)) {
            logger.error("week为空或null");
        }
        try {
            //查询每周各责任部门目标
            list = repository每周各责任部门目标.findCountByPermitAndYMAndCarCodeAndWeek(permit, yearMonth, carCode, week);
            List listMonth = repository每月各责任部门目标.findModelByPermitAndYMAndCarCode(permit, yearMonth, carCode);
            //判断 week是否保存目标，如果未保存，获取剩余天数的平均值 * week天数  作为week默认目标
            if ((list == null || list.size() < 1) && (listMonth != null && listMonth.size() > 0)) {
                //查询本月每周天数
                List<Map> listWeek = WeekOfMonthUtils.planWeek(yearMonth, 0);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(sdf.parse(yearMonth));
                //获取本月天数
                int days = calendar.getActualMaximum(Calendar.DATE);
                //判断参数week是否超过本月周数
                if (listWeek.size() < Integer.parseInt(week)) {
                    resultData.setMessage("本月没有" + week + "周");
                    return resultData;
                }
                Double surplusDays = Double.parseDouble(days + "");
                Double daysOfWeek = Double.parseDouble(listWeek.get(Integer.parseInt(week) - 1).get("天数").toString());
                //查询已存的周数
                List listMaxWeek = crossDepartmentTacticsRepository.findMaxWeekAboutDepartment(permit, yearMonth, carCode);
                int weekNum = 0;
                if (listMaxWeek != null) {
                    weekNum = Integer.parseInt(((Map) listMaxWeek.get(0)).get("第N周").toString());
                }
                for (int i = 0; i < weekNum; i++) {
                    surplusDays = surplusDays - Integer.parseInt(listWeek.get(i).get("天数").toString());
                }
                //week周的天数 / 本月未设定目标天数
                Double rate = daysOfWeek / surplusDays;
                list = crossDepartmentTacticsRepository.findWeekDepartmentTarget(permit, yearMonth, carCode, week, rate);
            }
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 0));
        } catch (Exception e) {
            logger.error("查询周结果责任部门目标", e);
        }
        return resultData;
    }

    public ResultData findWeekSalesTarget(String permit, String yearMonth, String carCode, String week) {
        ResultData resultData = new ResultData("false", null, null);
        List<?> list = null;
        if (yearMonth == null || "".equals(yearMonth)) {
            logger.error("yearMonth为空或null");
        }
        if (carCode == null || "".equals(carCode)) {
            logger.error("carCode为空或null");
        }
        if (week == null || "".equals(week)) {
            logger.error("week为空或null");
        }
        try {
            list = repository每周销售顾问目标.findCountByPermitAndYMAndCarCodeAndWeek(permit, yearMonth, carCode, week);
            List listMonth = repository每月销售顾问目标.findModelByPermitAndYMAndCarCode(permit, yearMonth, carCode);
            if ((list == null || list.size() < 1) && (listMonth != null && listMonth.size() > 0)) {
                List<Map> listWeek = WeekOfMonthUtils.planWeek(yearMonth, 0);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(sdf.parse(yearMonth));
                int days = calendar.getActualMaximum(Calendar.DATE);
                if (listWeek.size() < Integer.parseInt(week)) {
                    resultData.setMessage("本月没有" + week + "周");
                    return resultData;
                }
                Double surplusDays = Double.parseDouble(days + "");
                Double daysOfWeek = Double.parseDouble(listWeek.get(Integer.parseInt(week) - 1).get("天数").toString());
                List listMaxWeek = crossDepartmentTacticsRepository.findMaxWeekAboutSales(permit, yearMonth, carCode);
                int weekNum = 0;
                if (listMaxWeek != null) {
                    weekNum = Integer.parseInt(((Map) listMaxWeek.get(0)).get("第N周").toString());
                }
                for (int i = 0; i < weekNum; i++) {
                    surplusDays = surplusDays - Integer.parseInt(listWeek.get(i).get("天数").toString());
                }

                Double rate = daysOfWeek / surplusDays;
                list = crossDepartmentTacticsRepository.findWeekSalesTarget(permit, yearMonth, carCode, week, rate);
            }
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 0));
        } catch (Exception e) {
            logger.error("查询周结果责任部门目标", e);
        }
        return resultData;
    }


    public ResultData getWeekOfAttachers(String permit, RealInfo realInfo) {
        ResultData resultData = new ResultData();

        String year = realInfo.year;
        String month = !StringUtils.isNullOrBlank(realInfo.beginDay) ? realInfo.beginDay.substring(4,6) : realInfo.month;
        String carCode = realInfo.carCode;
        String beginDay = realInfo.beginDay;
        String endDay = realInfo.endDay;
        Integer day = Integer.parseInt(realInfo.days);//本周天数


        if (realInfo.carCode != null && realInfo.carCode.equals("总计") || realInfo.carCode.equals("合计")) {
            carCode = "";
        }
        String groupType = realInfo.groupType;

        try {
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();

            calendar.setTime(sdf2.parse(beginDay));
            Integer days = calendar.getActualMaximum(Calendar.DATE);//本月天数
            Map map = new HashMap();
            String ym = beginDay.substring(0, 6);

            // 依据年月获取日期范围
            String month_Begin_Date = getMonthDateRange(year,month,0);
            String month_End_Date = getMonthDateRange(year,month,1);

            Map mObj;
            String channel;
            List resultSet = crossDepartmentTacticsRepository.getWeekClueNum(permit, month_Begin_Date, beginDay, endDay, groupType, day, days);
            List recommends = new ArrayList<>(), repurchases = new ArrayList<>(), nets = new ArrayList<>(), incalls = new ArrayList<>(), gathers = new ArrayList<>();
            for(Object rs : resultSet){
                mObj = (Map) rs;
                if(mObj.get("渠道") == null || mObj.get("渠道").toString().equals("")) continue;

                channel = mObj.get("渠道").toString() + "线索数";
                mObj.put(channel, mObj.get("有效线索数"));
                if(mObj.get("渠道").toString().equals("推荐")){
                    recommends.add(mObj);
                }else if(mObj.get("渠道").toString().equals("再购")){
                    repurchases.add(mObj);
                }else if(mObj.get("渠道").toString().equals("网络")){
                    nets.add(mObj);
                }else if(mObj.get("渠道").toString().equals("主动集客")){
                    gathers.add(mObj);
                }else if(mObj.get("渠道").toString().equals("呼入")){
                    incalls.add(mObj);
                }
                mObj.remove("有效线索数");
                mObj.remove("渠道");
            }
            map.put("推荐线索数", recommends);
            map.put("再购线索数", repurchases);
            map.put("网络线索数", nets);
            map.put("主动集客线索数", gathers);
            map.put("呼入线索数", incalls);

            /*map.put("推荐线索数", crossDepartmentTacticsRepository.findCountByPermitAndYMAndCarCodeWeek(permit, beginDay, endDay, carCode, groupType, "推荐", day, days));
            map.put("再购线索数", crossDepartmentTacticsRepository.findCountByPermitAndYMAndCarCodeWeek(permit, beginDay, endDay, carCode, groupType, "再购", day, days));
            map.put("网络线索数", crossDepartmentTacticsRepository.findCountByPermitAndYMAndCarCodeWeek(permit, beginDay, endDay, carCode, groupType, "网络", day, days));
            map.put("主动集客线索数", crossDepartmentTacticsRepository.findCountByPermitAndYMAndCarCodeWeek(permit, beginDay, endDay, carCode, groupType, "主动集客", day, days));
            map.put("呼入线索数", crossDepartmentTacticsRepository.findCountByPermitAndYMAndCarCodeWeek(permit, year, beginDay, endDay, groupType, "呼入", day, days));
            */

            resultSet = crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "推荐,再购,呼入,网络,主动集客", carCode, groupType, beginDay, endDay);
            List recommendInrooms = new ArrayList<>(), repurchaseInroom = new ArrayList<>(),incallInrooms = new ArrayList<>(), netInroom = new ArrayList<>(), gatherInroom = new ArrayList<>();

            for(Object rs : resultSet){
                mObj = (Map) rs;
                if(mObj.get("顾客来源") == null || mObj.get("顾客来源").toString().equals("")) continue;

                channel = mObj.get("顾客来源").toString() + "进店线索数";
                mObj.put(channel, mObj.get("进店线索数"));
                if(mObj.get("顾客来源").toString().equals("推荐")){
                    recommendInrooms.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("再购")){
                    repurchaseInroom.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("网络")){
                    netInroom.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("主动集客")){
                    gatherInroom.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("呼入")){
                    incallInrooms.add(mObj);
                }
                mObj.remove("进店线索数");
                mObj.remove("顾客来源");
            }

            map.put("推荐进店线索数", recommendInrooms);
            map.put("再购进店线索数", repurchaseInroom);
            map.put("呼入进店线索数", incallInrooms);
            map.put("网络进店线索数", netInroom);
            map.put("主动集客进店线索数", gatherInroom);

            /*
            *//* 推荐进店线索数*//*
            map.put("推荐进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "推荐", carCode, groupType, beginDay, endDay));
           *//* 再购进店线索数*//*
            map.put("再购进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "再购", carCode, groupType, beginDay, endDay));
           *//* 呼入进店线索数*//*
            map.put("呼入进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "呼入", carCode, groupType, beginDay, endDay));
            *//*网络进店线索数*//*
            map.put("网络进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "网络", carCode, groupType, beginDay, endDay));
            *//* 主动集客进店线索数*//*
            map.put("主动集客进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "主动集客", carCode, groupType, beginDay, endDay));
            */

            List list = new ArrayList();
            list.add(map);
            PageData pageData = new PageData(list, 0);
            resultData.setResult("true");
            resultData.setPageData(pageData);
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("程序异常");
            logger.error("查询跨部门周实际数据异常", e);
        }
        return resultData;

    }

    public ResultData getMonthOfAttachers(String permit, RealInfo realInfo) {
        ResultData resultData = new ResultData();

        String year = realInfo.year;
        String month = realInfo.month;
        String carCode = realInfo.carCode;
        String beginDay = realInfo.beginDay;
        String endDay = realInfo.endDay;
        if (realInfo.carCode != null && realInfo.carCode.equals("总计")) {
            carCode = "";
        }
        String groupType = realInfo.groupType;

        try {
            Map map = new HashMap();
            String ym = null;
            if (month != null && month.length() == 1) {
                ym = year + "0" + month;
            } else {
                ym = year + month;
            }


            // 依据年月获取日期范围
            String month_Begin_Date = getMonthDateRange(year,month,0);
            String month_End_Date = getMonthDateRange(year, month, 1);

            List resultSet = crossDepartmentTacticsRepository.getClueNumOfMonth(permit, month_Begin_Date, groupType);
            List recommendInrooms = new ArrayList<>(), repurchaseInroom = new ArrayList<>(),incallInrooms = new ArrayList<>(), netInroom = new ArrayList<>(), gatherInroom = new ArrayList<>();
            Map mObj;
            String channel;
            for(Object rs : resultSet){
                mObj = (Map) rs;
                if(mObj.get("顾客来源") == null || mObj.get("顾客来源").toString().equals("")) continue;

                channel = mObj.get("顾客来源").toString() + "进店线索数";
                mObj.put(channel, mObj.get("进店线索数"));
                if(mObj.get("顾客来源").toString().equals("推荐")){
                    recommendInrooms.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("再购")){
                    repurchaseInroom.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("网络")){
                    netInroom.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("主动集客")){
                    gatherInroom.add(mObj);
                }else if(mObj.get("顾客来源").toString().equals("呼入")){
                    incallInrooms.add(mObj);
                }
                mObj.remove("进店线索数");
                mObj.remove("顾客来源");
            }
            map.put("推荐进店线索数", recommendInrooms);
            map.put("再购进店线索数", repurchaseInroom);
            map.put("呼入进店线索数", incallInrooms);
            map.put("网络进店线索数", netInroom);
            map.put("主动集客进店线索数", gatherInroom);

            /* 推荐进店线索数*//*
            map.put("推荐进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "推荐", carCode, groupType, beginDay, endDay));
           *//* 再购进店线索数*//*
            map.put("再购进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "再购", carCode, groupType, beginDay, endDay));
           *//* 呼入进店线索数*//*
            map.put("呼入进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "呼入", carCode, groupType, beginDay, endDay));
            *//*网络进店线索数*//*
            map.put("网络进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "网络", carCode, groupType, beginDay, endDay));
            *//* 主动集客进店线索数*//*
            map.put("主动集客进店线索数", crossDepartmentTacticsRepository.getEnterShopCountCDO(permit, month_Begin_Date, month_End_Date, "主动集客", carCode, groupType, beginDay, endDay));
*/

            List list = new ArrayList();
            list.add(map);
            PageData pageData = new PageData(list, 0);
            resultData.setResult("true");
            resultData.setPageData(pageData);
        } catch (Exception e) {
            resultData.setResult("false");
            resultData.setMessage("程序异常");
            logger.error("查询跨部门上月实际数据异常", e);
        }
        return resultData;
    }

    public ResultData findEveryWeekOfTarget(String permit, GetYMAndCarCode getYMAndCarCode) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            List<每周设定目标Model> list = repository每周设定目标.findEveryWeekOfTarget(permit, getYMAndCarCode.yearMonth, getYMAndCarCode.week, getYMAndCarCode.carCode);
            resultData.setResult("true");
            resultData.setPageData(new PageData(list, 1));
        } catch (Exception e) {
            resultData.setMessage("程序异常");
            logger.error("查询每周设定目标异常", e);
        }
        return resultData;
    }

    public ResultData saveEveryWeekOgTarget(String permit, String username, GetMonthSalesTarget getMonthSalesTarget) {
        ResultData resultData = new ResultData("false", null, null);
        try {
            List<每周设定目标Model> list = repository每周设定目标.findEveryWeekOfTarget(permit, getMonthSalesTarget.年月, getMonthSalesTarget.第N周, getMonthSalesTarget.车型);
            每周设定目标Model model = null;
            if (list != null && !list.isEmpty()) {
                model = list.get(0);
            } else {
                model = new 每周设定目标Model();
            }
            save每周设定目标(model, permit, username, getMonthSalesTarget);
            resultData.setResult("true");
        } catch (Exception e) {
            resultData.setMessage("程序异常");
            logger.error("保存每周设定目标异常", e);
        }
        return resultData;
    }

    private void save每周设定目标(每周设定目标Model model, String permit, String username, GetMonthSalesTarget getMonthSalesTarget) {
        model.set许可(permit);
        model.set年月(getMonthSalesTarget.年月);
        model.set第N周(getMonthSalesTarget.第N周);
        model.set车型(getMonthSalesTarget.车型);
        model.set交车数(getMonthSalesTarget.交车数);
        model.set订单数(getMonthSalesTarget.订单数);
        model.set报价数(getMonthSalesTarget.报价数);
        model.set进店线索数(getMonthSalesTarget.进店线索数);
        model.set总线索数(getMonthSalesTarget.总线索数);
        model.set活跃进店线索数(getMonthSalesTarget.活跃进店线索数);
        model.set休眠进店线索数(getMonthSalesTarget.休眠进店线索数);
        model.set推荐进店线索数(getMonthSalesTarget.推荐进店线索数);
        model.set再购进店线索数(getMonthSalesTarget.再购进店线索数);
        model.set散客进店线索数(getMonthSalesTarget.散客进店线索数);
        model.set呼入进店线索数(getMonthSalesTarget.呼入进店线索数);
        model.set网络进店线索数(getMonthSalesTarget.网络进店线索数);
        model.set主动集客进店线索数(getMonthSalesTarget.主动集客进店线索数);
        model.set活跃线索数(getMonthSalesTarget.活跃线索数);
        model.set休眠线索数(getMonthSalesTarget.休眠线索数);
        model.set推荐线索数(getMonthSalesTarget.推荐线索数);
        model.set再购线索数(getMonthSalesTarget.再购线索数);
        model.set散客线索数(getMonthSalesTarget.散客线索数);
        model.set呼入线索数(getMonthSalesTarget.呼入线索数);
        model.set网络线索数(getMonthSalesTarget.网络线索数);
        model.set主动集客线索数(getMonthSalesTarget.主动集客线索数);
        model.set试驾数(getMonthSalesTarget.试驾数);
        model.set提交日期(new Timestamp(new Date().getTime()));
        model.set更新账号(username);
        model.set更新时间(new Timestamp(new Date().getTime()));
        repository每周设定目标.save(model);
    }

    /**
     * 依据年月获取日期范围
     * @param year 2017
     * @param month 9
     * @param type 0-月初／1-月末
     * @return
     */
    public String getMonthDateRange(String year,String month,Integer type){
        String reStr = null;

        if (Strings.isNullOrEmpty(year) || Strings.isNullOrEmpty(month))
        {
            return  reStr;
        }

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR,Integer.valueOf(year));
        calendar.set(Calendar.MONTH,Integer.valueOf(month)-1);
        if (type==0){
            // 获取月第一天
            calendar.set(Calendar.DAY_OF_MONTH,1);
        } else {
            // 获取月最后一天
            calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        }
        reStr = sdf2.format(calendar.getTime());
        return reStr;
    }

    private Map getActiveClueNum (Map map,List resultSet) {
        List cusMaps = new ArrayList<>(), archiveMaps = new ArrayList<>(), activeInrooms = new ArrayList<>(),
                sleepingInroom = new ArrayList<>(), recommendInrooms = new ArrayList<>(), repurchaseInrooms = new ArrayList<>(),
                incallInrooms = new ArrayList<>(), netInrooms = new ArrayList<>(), gatherInrooms = new ArrayList<>();
        for(Object rs : resultSet) {
            Map mObj = (Map) rs;
            if("散客".equals(mObj.get("渠道"))) {
                //散客客流
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("散客客流数", mObj.get("展厅客流数"));
                cusMaps.add(cusMap);
                //散客留档
                Map archiveMap = new HashMap<>();
                archiveMap.put("销售顾问", mObj.get("销售顾问"));
                archiveMap.put("散客留档数", mObj.get("首次进店线索数"));
                archiveMaps.add(archiveMap);
            } else if ("活跃".equals(mObj.get("渠道"))){
                //活跃线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("活跃进店线索数", mObj.get("再次进店线索数"));
                activeInrooms.add(cusMap);
            } else if ("休眠".equals(mObj.get("渠道"))){
                //休眠线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("休眠进店线索数", mObj.get("再次进店线索数"));
                sleepingInroom.add(cusMap);
            } else if ("推荐".equals(mObj.get("渠道"))){
                //推荐线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("推荐进店线索数", mObj.get("首次进店线索数"));
                recommendInrooms.add(cusMap);
            } else if ("再购".equals(mObj.get("渠道"))){
                //再购线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("再购进店线索数", mObj.get("首次进店线索数"));
                repurchaseInrooms.add(cusMap);
            } else if ("网络".equals(mObj.get("渠道"))){
                //网络线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("网络进店线索数", mObj.get("首次进店线索数"));
                netInrooms.add(cusMap);
            } else if ("呼入".equals(mObj.get("渠道"))){
                //呼入线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("呼入进店线索数", mObj.get("首次进店线索数"));
                incallInrooms.add(cusMap);
            } else if ("主动集客".equals(mObj.get("渠道"))){
                //主动集客线索进店数
                Map cusMap = new HashMap<>();
                cusMap.put("销售顾问", mObj.get("销售顾问"));
                cusMap.put("主动集客进店线索数", mObj.get("首次进店线索数"));
                gatherInrooms.add(cusMap);
            }
        }
        map.put("散客客流数", cusMaps);
        map.put("散客留档数", archiveMaps);
        map.put("活跃进店线索数", activeInrooms);
        map.put("休眠进店线索数", sleepingInroom);
        map.put("推荐进店线索数", recommendInrooms);
        map.put("再购进店线索数", repurchaseInrooms);
        map.put("呼入进店线索数", incallInrooms);
        map.put("网络进店线索数", netInrooms);
        map.put("主动集客进店线索数", gatherInrooms);
        return map;
    }

    private Map getOtherNum (Map map, List resultSet) {
        List testDriveList = new ArrayList(), quoteList = new ArrayList(), orderList = new ArrayList(), carSaleList = new ArrayList();
        for (Object o : resultSet) {
            Map mObj = (Map) o;
            Map testDriveMap = new HashMap(), quoteMap = new HashMap(), orderMap = new HashMap(), carSaleMap = new HashMap();
            testDriveMap.put("首次进店试驾数", mObj.get("首次试驾数"));
            testDriveMap.put("销售顾问", mObj.get("销售顾问"));
            testDriveList.add(testDriveMap);

            quoteMap.put("报价数", mObj.get("报价数"));
            quoteMap.put("销售顾问", mObj.get("销售顾问"));
            quoteList.add(quoteMap);

            orderMap.put("订单数", mObj.get("订单数"));
            orderMap.put("销售顾问", mObj.get("销售顾问"));
            orderList.add(orderMap);

            carSaleMap.put("交车数", mObj.get("交车数"));
            carSaleMap.put("销售顾问", mObj.get("销售顾问"));
            carSaleList.add(carSaleMap);
        }
        map.put("首次进店试驾数", testDriveList);
        map.put("交车数", carSaleList);
        map.put("订单数",orderList);
        map.put("报价数", quoteList);
        return map;
    }

}
